<template>
  <div class="box0">
    <button class="jian" @click="handleSub">-</button>
    <input type="text" @change="handlePost" :value="value" />
    <button class="jia" @click="handleAdd">+</button>
  </div>
</template>

<script>
export default {
  name: 'QuanTity',
  props: {
    value: {
      type: Number,
      default: 1
    }
  },
  methods: {
    handleSub () {
      if (this.value <= 1) {
        return
      }
      this.$emit('input', this.value - 1)
    },
    handleAdd () {
      this.$emit('input', this.value + 1)
    },
    handlePost (e) {
      const num = +e.target.value
      if (isNaN(num) || num < 1) {
        e.target.value = this.value
        return
      }
      this.$emit('input', num)
    }
  }
}
</script>

<style lang="less">
.box0 {
  display: flex;
  .jian{
    width: 34px;
    height: 34px;
    background-color: #F2F3F5;
    text-align: center;
    border: none;
    line-height: 34px;
  }
  .jia{
    width: 34px;
    height: 34px;
    background-color: #F2F3F5;
    text-align: center;
    line-height: 34px;
    border: none;
  }
  input{
    width: 40px;
    border: none;
    margin: 0 10px;
    background-color: #F2F3F5;
    text-align: center;
  }
}
</style>
